let flat = [1, 2, [1, [2, [3]]], 4, 5, [2, 3, 4, 5, [2, 3, 4], 22], 1]

const myFlat = (list, depth = 1) => {
    let result = []
    list.forEach(item => {
        if (Array.isArray(item) && depth > 0) {
            result.push(...myFlat(item, depth - 1))
        } else {
            result.push(item)
        }
    });
    return result
}
console.log(myFlat(flat))